﻿using QuanLyKho.DTO;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using System.Linq;
using System.Text;

namespace QuanLyKho.DAL
{
    class KhoDAO : DataProvider
    {
        public ArrayList getListKho()
        {
            string sqlString = "Select * from Kho";
            adapter = new OleDbDataAdapter(sqlString, connection);
            DataSet dataSet = new DataSet();
            adapter.Fill(dataSet);
            ArrayList listHangHoa = ConvertDataSetToArrayList(dataSet);

            return listHangHoa;
        }

        protected override object GetDataFromDataRow(DataTable dt, int i)
        {
            KhoDTO khoDTO = new KhoDTO();
            khoDTO.MaKho = dt.Rows[i]["MaKho"].ToString();
            khoDTO.TenKho = dt.Rows[i]["TenKho"].ToString();

            return (object) khoDTO;
        }

        public void insert(KhoDTO khoDTO)
        {
            string sqlString = "insert into Kho values('"
                + khoDTO.MaKho + "','"
                + khoDTO.TenKho + "','"
                + 0 + "')";
            ExecuteNonQuery(sqlString);
        }

        public ArrayList getListKhoByMaHang(string maHang)
        {
            string sqlString = "Select DISTINCT K.MaKho, K.TenKho from Kho K, PhieuNhap PN"
                + " WHERE K.MaKho = PN.MaKho AND PN.MaHang = '" + maHang + "'";
            adapter = new OleDbDataAdapter(sqlString, connection);
            DataSet dataSet = new DataSet();
            adapter.Fill(dataSet);
            ArrayList listKho = ConvertDataSetToArrayList(dataSet);

            return listKho;
        }
    }
}
